//
//  TabbarView.m
//  YXLTabbar
//
//  Created by 李庆春 on 15/5/18.
//  Copyright (c) 2015年 YXL. All rights reserved.
//

//动态获取设备高度
#define IPHONE_HEIGHT [UIScreen mainScreen].bounds.size.height
//动态获取设备宽度
#define IPHONE_WIDTH [UIScreen mainScreen].bounds.size.width
#import "TabbarView.h"

@implementation TabbarView
{
    UIButton *midBtn ;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatData];
        [self creatUI];
    }
    return self;
}

- (void)creatData
{
    _imageNormalAry = @[@"shouye_1",@"jiaoliu_1",@"wode_1"];
    
    _imageHightlightAry = @[@"shouye_",@"jiaoliu_",@"wode_"];
}

- (void)creatUI
{
    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:93/255.0 green:152/255.0 blue:253/255.0 alpha:1.0]];
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 49)];
    _imageView.userInteractionEnabled = YES;
    _imageView.backgroundColor = [UIColor whiteColor];
    _imageView.image = [UIImage imageNamed:@"tabbar.png"];
    [self addSubview:_imageView];

    for (int i = 0; i < 3; i ++)
    {
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 1001+i;
        btn.frame = CGRectMake(i*IPHONE_WIDTH/3, 0, IPHONE_WIDTH/3, 49);
        
        [btn setImage:[UIImage imageNamed:_imageNormalAry[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:_imageHightlightAry[i]] forState:UIControlStateSelected];
        
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i == 0) {
            btn.selected = YES;
            midBtn = btn;
        }
        [_imageView addSubview:btn];
    }
    
}

-(void)btnClick:(UIButton *)sender
{
    midBtn.selected = NO;
    midBtn = sender;
    midBtn.selected = YES;
    // NSLog(@"%d",sender.selected);
    if (self.sendBlock) {
        self.sendBlock(sender);
    }
}

@end
